package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    @Override   //查询地址列表
    public List<Address> findList() {
        //查询当前顾客的地址簿
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());
        List<Address> addressList = addressMapper.selectList(addressLambdaQueryWrapper);
        return addressList;
    }

    @Override   //新增地址
    public void save(Address address) {
        address.setUserId(UserHolder.get().getId());

        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());

        //如果当前客户地址薄中还没有地址,则新建这个地址设为默认地址
        int count = addressMapper.selectCount(addressLambdaQueryWrapper);
        if (count == 0) {
            address.setIsDefault(1);
        }

        addressMapper.insert(address);
        //结果: 每个顾客的地址薄中,要么没有地址,要么有且只有一个默认地址
    }

    @Override   //设置当前地址为默认地址
    public void altDefault(Long id) {
        //由于设置默认地址的请求只会从已有的地址信息中发出,所以只需要修改原有的默认地址,和当前的地址即可
        //1.找到默认地址,将其修改为非默认地址
        Address address = new Address();
        address.setIsDefault(0);//设置为非默认地址

        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //条件: where is_default = 1 and user_Id = 当前用户
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());
        addressLambdaQueryWrapper.eq(Address::getIsDefault, 1);//默认地址
        addressMapper.update(address, addressLambdaQueryWrapper);
        //UPDATE address_book SET is_default=?, update_time=?, update_user=? WHERE (user_id = ? AND is_default = ?)

        //2.设置当前地址为默认地址
        address.setId(id);
        address.setIsDefault(1);//设置当前地址为默认地址
        //update address_book SET is_default = 1 where id = 当前用户
        addressMapper.updateById(address);//根据id更新该地址
        //UPDATE address_book SET is_default=?, update_time=?, update_user=? WHERE id=?


//         //TODO 这样子做每次修改默认地址,所有的地址更新时间,修改人都会变....
//        //1.将当前顾客地址薄中的地址都设为非默认地址
//        Address address = new Address();
//        address.setIsDefault(0);//设置当前地址为非默认地址
//        LambdaUpdateWrapper<Address> addressLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
//        addressLambdaUpdateWrapper.eq(Address::getUserId, UserHolder.get().getId());//当前顾客的地址薄
//        addressMapper.update(address, addressLambdaUpdateWrapper);
//        //1.设置当前地址为默认地址
//        address.setId(id);
//        address.setIsDefault(1);//设置当前地址为默认地址
//        addressMapper.updateById(address);//根据id更新该地址


//        //查找当前顾客地址薄中是否已经有默认地址了
//        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());//当前顾客
//        addressLambdaQueryWrapper.eq(Address::getIsDefault, 1);//默认地址
//        Address addressFromDB = addressMapper.selectOne(addressLambdaQueryWrapper);
//        if (addressFromDB != null) {
//            //已有默认地址
//            addressFromDB.setIsDefault(0);//将该地址设为非默认地址
//            addressMapper.updateById(addressFromDB);//更新该地址
//        }
//        //根据id找到要修改的地址
//        Address address = addressMapper.selectById(id);
//        address.setIsDefault(1);//设置为默认地址
//        addressMapper.updateById(address);//更新该地址
    }

    @Override    //查询默认地址
    public Address findDefault() {
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressLambdaQueryWrapper.eq(Address::getUserId, UserHolder.get().getId());
        addressLambdaQueryWrapper.eq(Address::getIsDefault, 1);//默认地址

        Address address = addressMapper.selectOne(addressLambdaQueryWrapper);
        return address;
    }

    @Override    //回显当前地址(修改用)  根据id查询
    public Address findById(Long id) {
        return addressMapper.selectById(id);
    }

    @Override   //修改地址
    public void updateById(Address address) {
        addressMapper.updateById(address);
    }

    @Override   //删除地址
    public void deleteByIds(Long[] ids) {


        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
//                //查看是否是默认地址  默认地址禁止删除
//                Address address = addressMapper.selectById(id);
//                if(address == null || address.getIsDefault() == 1){
//                    throw new CustomException("地址删除失败");
//                }

                addressMapper.deleteById(id);
            }
        }
    }
}
